package com.kaifamiao.wendao.utils;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;

public class DataSourceFactory {

    //用来缓存单一实例的类变量
    private final static DataSourceFactory factory = new DataSourceFactory();

    //将构造方法私有化
    private DataSourceFactory() {
        super();
    }

    // 提供一个可以返回单个实例的类方法
    public static DataSourceFactory getInstance(){
        return factory;
    }

    //用来缓存数据源的实例变量
    private DruidDataSource dataSource;

    //初始化数据源
    public void init (){
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/wendao?serverTimezone=Asia/Chongqing");
        dataSource.setUsername("root");
        dataSource.setPassword("");
    }
     public DataSource getDataSource(){
        return dataSource;
     }

     //关闭数据源
    public void close(){
        if (dataSource!=null){
            dataSource.close();
        }
    }
}
